package com.amtf.datafile3.cfg.std;

import com.amtf.datafile3.DataException;
import com.amtf.datafile3.DataField;
import com.amtf.datafile3.DataFieldDefinition;
import com.amtf.datafile3.DataFieldParser;
import com.amtf.datafile3.DataFieldRender;
import com.amtf.datafile3.DataFieldType;
import com.amtf.datafile3.core.std.parser.Object2StringFieldParser;
import com.amtf.datafile3.core.std.render.StringFieldRender;
import com.amtf.datafile3.core.std.type.StringFieldType;

public abstract class StdDataFieldDefinition extends StdDefinition implements DataFieldDefinition {

	protected DataFieldType type = new StringFieldType();
	protected DataFieldParser parser = new Object2StringFieldParser();
	protected DataFieldRender render = new StringFieldRender();
	protected String description = "";

	public abstract DataFieldDefinition clone();
	
	public StdDataFieldDefinition(String name, DataFieldType type, DataFieldParser parser, DataFieldRender render) {
		super(name);
		this.type = type;
		this.render = render;
		this.parser = parser;
	}

	public void copyTo(StdDataFieldDefinition newObj) {
		newObj.name = this.name;
		newObj.type = this.type.clone();
		newObj.parser = this.parser.clone();
		newObj.render = this.render.clone();
		newObj.description = this.description;
	}

//	public DataFieldDefinition clone() {
//		StdDataFieldDefinition newObj = new StdDataFieldDefinition(null, null, null, null);
//		copyTo(newObj);
//		return newObj;
//	}

	public DataField newDataFieldInstance() {
		return type.newField(this);
	}

	@Override
	public boolean equals(Object obj) {
		StdDataFieldDefinition f1 = (StdDataFieldDefinition) obj;

		return f1.name.equals(this.name);
	}

	public void validate(DataField value) throws DataException {
		type.validate(value);
	}

	public DataField parse(Object value) throws DataException {
		return parser.parse(this, value);
	}

	public String rending(DataField value) {
		return render.rending(value);
	}

	public DataFieldParser getParser() {
		return parser;
	}

//	public void setParser(DataFieldParser parser) {
//		this.parser = parser;
//	}

	public DataFieldRender getRender() {
		return render;
	}

//	public void setRender(DataFieldRender Render) {
//		this.render = Render;
//	}

	public DataFieldType getType() {
		return type;
	}

//	public void setType(DataFieldType type) {
//		this.type = type;
//	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

}
